<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      检查单{{ createStatus ? '新增' : '更新' }}
    </div>
    <el-row :gutter="10">
      <el-form :model="inspectForm" ref="inspectForm" :rules="inspectFromRules" label-width="100px">
          <el-col :span="12">
            <el-form-item label="检查单名称" prop="title">
              <el-input v-model="inspectForm.title" placeholder="请输入检查单名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业主单位" prop="inspectEnterpriseId">
              <el-select v-model="inspectForm.inspectEnterpriseId" placeholder="请选择业主单位" style="width: 100%" clearable filterable @change="handleChange">
                <el-option v-for="(item,index) in enterpriseDominantList" :key="index" :value="item.ucId"
                           :label="item.name"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查人" prop="inspectPersonId">
              <el-select v-model="inspectForm.inspectPersonId" placeholder="请选择执行人员" style="width:100%">
                <el-option v-for="(item,index) in userList" :key="index" :value="item.ucId"
                           :label="item.name"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        <el-col :span="12">
            <el-form-item label="检查时间" prop="inspectTime">
              <el-date-picker
                  v-model="inspectForm.inspectTime"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

      </el-form>
    </el-row>


    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelEnterprise">取 消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createInspect">创 建
      </el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updateEnterprise">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {addData, updateData, getData} from "@/api/inspect";
import {getCommanderList} from "@/api/user";
import {getToken} from "@/utils/auth";
import {getInEnterpriseYZList} from "@/api/enterpriseDominant";

export default {
  name: "userEdit",
  components: {},
  data() {
    return {
      postShow: false,
      inspectForm: {
        ucId: '',
        title: '',
        inspectEnterpriseId: '',
        inspectEnterpriseName: '',
        inspectPersonId: '',
        inspectPersonName: '',
        inspectTime: '',
        status: ''
      },
      inspectFromRules: {
        title: [{required: true, message: '检查单名称不能为空', trigger: 'blur'}],
        inspectEnterpriseId: [{required: true, message: '业主单位不能为空', trigger: 'blur'}]
      },
      addLoading: false,
      userList: [],
      equipmentTypeList: [],
      enterpriseDominantList: []
    }
  },
  computed: {
    createStatus() {
      return this.$route.params.ucId == '0'
    },
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.getInEnterpriseYZList()
    this.getCommanderList()
    if(this.$route.params.ucId != '0') {
      this.init()
    }
  },
  methods: {
    handleChange(value){
      console.log(value)
    },
    cancelEnterprise() {
      window.history.back()
    },
    init() {
      getData(this.$route.params.ucId).then(res => {
        this.inspectForm = res.data.data
      }).catch(error => {

      })
    },
    createInspect() {
      this.addLoading = true
      this.$refs['inspectForm'].validate(valid => {
        if (valid) {
          addData(this.inspectForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "检查单添加成功"
            })
            this.cancelEnterprise()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateEnterprise() {
      this.addLoading = true
      this.$refs['inspectForm'].validate(valid => {
        if (valid) {
          updateData(this.inspectForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "检查单更新成功"
            })
            this.cancelEnterprise()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    getCommanderList() {
      getCommanderList({deleted: false}).then(res => {
        this.userList = res.data
      }).catch(error => {

      })
    },
    getInEnterpriseYZList() {
      getInEnterpriseYZList({deleted: false}).then(res => {
        this.enterpriseDominantList = res.data
      }).catch(error => {

      })
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}

.avatar {
  width: 149px;
  height: 149px;
  display: block;
}
</style>
